package com.core.shared;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.*;
import com.google.common.primitives.Ints;

import java.util.*;

/**
 * @author liuxianfei@ttggapp.com
 * @date 2018/7/31 21:37
 */
public class GuavaDemo {

    public static void show() {
        //创建集合
        List<String> list = Lists.newArrayList();
        Map<String, String> map = Maps.newHashMap();
        Set<String> set = Sets.newHashSet();

        //拼接字符
        String[] names = {"123", "456", "789"};
        System.out.println(Joiner.on("---").join(names));

        //拆分字符
        String name = "123---456---789";
        Iterable<String> split = Splitter.on("---").split(name);
        for (Iterator it = split.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
        }

        //非空判断
        String notNull = "";
        Preconditions.checkNotNull(notNull);

        //集合求并集、交集
        HashSet<Integer> set1 = Sets.newHashSet(1, 2, 3);
        HashSet<Integer> set2 = Sets.newHashSet(3, 4, 5);
        Sets.SetView<Integer> union = Sets.union(set1, set2);
        //并集：[1, 2, 3, 4, 5]
        System.out.println(union.toString());
        Sets.SetView<Integer> difference = Sets.difference(set1, set2);
        //第一个集合独有的：[1, 2]
        System.out.println(difference.toString());
        Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);
        //交集：[3]
        System.out.println(intersection.toString());

        //基础类型计算
        int[] array = {1, 2, 3, 4, 5};
        System.out.println(Ints.contains(array, 2));
        System.out.println(Ints.max(array));
        System.out.println(Ints.min(array));
        System.out.println(Ints.compare(2, 1));

        //key对应多个value
        Multimap<String, String> multimap = ArrayListMultimap.create();
        multimap.put("lxf", "123");
        multimap.put("lxf", "456");
        //[123, 456]
        System.out.println(multimap.get("lxf"));

        //异常验证
        double money = -1;
        Preconditions.checkArgument(money > 0, "钱币必须大于0元: %s", money);

    }

}
